package plugins.AI;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;

public class FormDataScanPlugin extends AbstractOperationServicePlugIn implements IGPTAction {

    private static final Log logger = LogFactory.getLog(FormDataScanPlugin.class);

    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String, String> result = new HashMap<>();

        if ("generateDietPlan".equalsIgnoreCase(action)) {
            String currentName = params.get("currentName");

            if (currentName == null || currentName.trim().isEmpty()) {
                result.put("error", "参数 currentName 不能为空");
                return result;
            }

            // 1. 扫描用户基础资料 dcb4_userdata
            QFilter userFilter = new QFilter("name", QCP.equals, currentName);
            DynamicObjectCollection users = QueryServiceHelper.query("dcb4_userdata",
                    "name,dcb4_sex,dcb4_jiguan,dcb4_height,dcb4_weight,dcb4_warn,dcb4_bmi,dcb4_healthylevel",
                    new QFilter[]{userFilter});

            if (users.isEmpty()) {
                result.put("error", "未找到匹配的用户基础资料");
                return result;
            }

            if (users.size() > 1) {
                result.put("error", "找到多个匹配的用户基础资料，请提供唯一标识");
                return result;
            }

            DynamicObject user = users.get(0);

            // 2. 扫描订单单据 dcb4_order_history
            QFilter orderFilter = new QFilter("dcb4_user", QCP.equals, currentName);
            DynamicObjectCollection orders = QueryServiceHelper.query("dcb4_order_history",
                    "dcb4_user,dcb4_merchant,dcb4_dishes",
                    new QFilter[]{orderFilter});

            if (orders.isEmpty()) {
                result.put("error", "未找到匹配的订单单据");
                return result;
            }

            // 3. 扫描菜品基础资料 dcb4_dishes
            DynamicObjectCollection dishes = QueryServiceHelper.query("dcb4_dishes",
                    "dcb4_merchant,name,dcb4_material,dcb4_protein,dcb4_carbs,dcb4_fat,dcb4_vitamin,dcb4_na,dcb4_energy",
                    null);

            if (dishes.isEmpty()) {
                result.put("error", "未找到任何菜品基础资料");
                return result;
            }

            // 4. 结合用户信息、订单记录和菜品数据，生成饮食计划表
            StringBuilder dietPlan = new StringBuilder();
            dietPlan.append("用户信息:\n");
            dietPlan.append("姓名: ").append(user.getString("name")).append("\n");
            dietPlan.append("性别: ").append(user.getString("dcb4_sex")).append("\n");
            dietPlan.append("籍贯: ").append(user.getString("dcb4_jiguan")).append("\n");
            dietPlan.append("身高: ").append(user.getString("dcb4_height")).append("\n");
            dietPlan.append("体重: ").append(user.getString("dcb4_weight")).append("\n");
            dietPlan.append("忌口: ").append(user.getString("dcb4_warn")).append("\n");
            dietPlan.append("BMI: ").append(user.getString("dcb4_bmi")).append("\n");
            dietPlan.append("BMI等级: ").append(user.getString("dcb4_healthylevel")).append("\n\n");

            dietPlan.append("订单记录:\n");
            for (DynamicObject order : orders) {
                dietPlan.append("档口: ").append(order.getString("dcb4_merchant")).append(", ");
                dietPlan.append("菜品: ").append(order.getString("dcb4_dishes")).append("\n");
            }
            dietPlan.append("\n");

            dietPlan.append("菜品列表:\n");
            for (DynamicObject dish : dishes) {
                dietPlan.append("档口: ").append(dish.getString("dcb4_merchant")).append(", ");
                dietPlan.append("菜品名称: ").append(dish.getString("name")).append(", ");
                dietPlan.append("蛋白质: ").append(dish.getString("dcb4_protein")).append(", ");
                dietPlan.append("碳水化合物: ").append(dish.getString("dcb4_carbs")).append(", ");
                dietPlan.append("脂肪: ").append(dish.getString("dcb4_fat")).append(", ");
                dietPlan.append("维生素: ").append(dish.getString("dcb4_vitamin")).append(", ");
                dietPlan.append("钠: ").append(dish.getString("dcb4_na")).append(", ");
                dietPlan.append("热量: ").append(dish.getString("dcb4_energy")).append("\n");
            }

            result.put("dietPlan", dietPlan.toString());
        } else {
            result.put("error", "不支持的操作: " + action);
        }

        return result;
    }
}
